﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.Threading;
using System.IO;
using System.Windows.Forms;

namespace myking
{
    public static class ScriptRun
    {

        #region api申明
        [DllImport("user32.dll", EntryPoint = "keybd_event")]

        private static extern void keybd_event(

         byte bVk,    //虚拟键值

                    byte bScan,// 一般为0

                    int dwFlags,  //这里是整数类型  0 为按下，2为释放

                    int dwExtraInfo  //这里是整数类型 一般情况下设成为 0

                );
        /*
        使用方法：

            keybd_event((byte) Keys.LWin, 0, 0, 0);   //按下LWIN

        keybd_event((byte) Keys.D, 0, 0, 0);         //按下D

        keybd_event((byte) Keys.LWin, 0, 2, 0);   //释放LWIN

        keybd_event((byte) Keys.D, 0, 2, 0);        //释放D
*/

        [DllImport("User32.dll", EntryPoint = "PostMessage")]
        private static extern int PostMessage(IntPtr hWnd, int Msg, uint wParam, int t);

        #endregion

        #region 虚拟按键键值
        const byte VK_LBUTTON = 1;//鼠标左键
        const byte VK_RBUTTON = 2;//鼠标右键
        const byte VK_SHIFT = 16;//shift

        const int WM_CHAR = 0x0102;

        #endregion

        /// <summary>
        /// 调用系统dll控制键盘
        /// </summary>
        /// <param name="keycode">需要按住的键</param>
        /// <param name="flag">这里是整数类型  0 为按下，2为释放, 1点击?</param>
        public static void Keydown(string keycode,int flag)
        {
            byte bVk = 0;
            switch (keycode)
            {
                case "shift":
                    //bVk = (byte)Keys.LShiftKey;
                    bVk = VK_SHIFT;
                    break;
                case "left":
                    bVk = (byte)Keys.LShiftKey;
                    break;
                case "right":
                    bVk = (byte)Keys.RButton;
                    break;
                case "space":
                    bVk = (byte)Keys.Space;
                    break;
                case "esc":
                    bVk = (byte)Keys.Escape;
                    break;
                default:
                    break;
            }
            keybd_event(bVk, 0, flag, 0);
        }

        public static void KeydownPost(IntPtr programhWnd, string keycode)
        {
            byte bVk = 0;
            uint vk_f = 0;
            switch (keycode)
            {
                case "shift":
                    //bVk = (byte)Keys.LShiftKey;
                    bVk = VK_SHIFT;
                    break;
                case "left":
                    bVk = (byte)Keys.LShiftKey;
                    break;
                case "right":
                    bVk = (byte)Keys.RButton;
                    break;
                case "space":
                    bVk = (byte)Keys.Space;
                    vk_f = 32;
                    break;
                case "esc":
                    bVk = (byte)Keys.Escape;
                    break;
                default:
                    break;
            }
            PostMessage(programhWnd, WM_CHAR, vk_f, 0);
        }

    }
}